#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: style.py
# Description: 
#   编写 Unix 管道风格的 Python 代码
#           find            cat            grep               rsplit            
#   pattern ───> file names ───> all lines ───> filtered lines ───> last columns 
# Edit History: 
#   2011-10-24    File created.
#===============================================================================
import os
import fnmatch

def find(topdir, filepat):
    files = []
    for path, dirlist, filelist in os.walk(topdir):
        print(path, dirlist, filelist)
        for name in fnmatch.filter(filelist, filepat):
            files.append(name)
    return files
 
def cat(files):
   lines = []
   for file in files:
       with open(file) as f:
           for line in f:
               lines.append(line)
   return lines

def grep(pattern, lines):
   result = []
   import re
   pat = re.compile(pattern)
   for line in lines:
       if pat.search(line):
           result.append(line)
   return result

lines = grep('python', cat(find('./', '*.py')))
print("lines", lines)
col = (line.rsplit(None, 1)[1] for line in lines)
for c in col:
    print(c)
